package com.mydemo.admin.service.impl;

import com.mydemo.admin.mapper.SysMenuMapper;
import com.mydemo.admin.pojo.Meta;
import com.mydemo.common.pojo.Option;
import com.mydemo.admin.pojo.Router;
import com.mydemo.admin.pojo.SysMenu;
import com.mydemo.admin.service.ISysMenuService;
import com.mydemo.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;


/**
 * 菜单服务实现类
 *
 * @author Last_Island
 * @version 1.0
 */
@Service
public class SysMenuServiceImpl implements ISysMenuService {

    @Autowired
    private SysMenuMapper sysMenuMapper;

    /**
     * 获取菜单树形结构
     *
     * @return
     */
    @Override
    public List<SysMenu> getMenuList(SysMenu sysMenu) {
        List<SysMenu> menuList = sysMenuMapper.getMenuList(sysMenu);
        return getTreeMenuList(menuList);
    }

    /**
     * 获取树形选项数据
     *
     * @return
     */
    @Override
    public Option getTree() {
        List<SysMenu> menuList = getMenuList(null);
        Option option = new Option(0L, "菜单", new ArrayList<>());
        option.setChildren(getTreeOption(menuList));
        return option;
    }

    /**
     * 获取树形选项数据
     *
     * @param menuList
     * @return
     */
    public List<Option> getTreeOption(List<SysMenu> menuList) {
        List<Option> options = new ArrayList<>();
        for (SysMenu menu : menuList) {
            List<Option> optionChildren = new ArrayList<>();
            List<SysMenu> children = menu.getChildren();
            if (!children.isEmpty()) {
                optionChildren.addAll(getTreeOption(children));
            }
            Option option = new Option();
            if (!optionChildren.isEmpty()) {
                option.setChildren(optionChildren);
            }
            option.setId(menu.getMenuId());
            option.setLabel(menu.getMenuName());
            options.add(option);
        }
        return options;
    }

    /**
     * 递归获取菜单树形结构
     *
     * @param menuList
     * @return
     */
    public List<SysMenu> getTreeMenuList(List<SysMenu> menuList) {
        List<SysMenu> treeList = new ArrayList<>();
        menuList.forEach(menu -> {
            Long parentId = menu.getParentId();
            // 查找出一级菜单及子菜单
            if (parentId == 0) {
                getMenuChildren(menuList, menu);
                treeList.add(menu);
            }
        });
        return treeList;
    }

    /**
     * @param menuList
     * @param fMenu
     */
    public void getMenuChildren(List<SysMenu> menuList, SysMenu fMenu) {
        List<SysMenu> children = new ArrayList<>();
        // 获取所有子菜单
        menuList.forEach(menu -> {
            if (Objects.equals(menu.getParentId(), fMenu.getMenuId())) {
                children.add(menu);
                getMenuChildren(menuList, menu);
            }
        });
        if (!children.isEmpty())
            fMenu.setChildren(children);
    }

    /**
     * 获取路由
     *
     * @param roleId
     * @return
     */
    @Override
    public List<Router> getRouter(Long roleId) {
        List<SysMenu> menuList;
        if (SecurityUtils.isAdmin()) {
            menuList = sysMenuMapper.getRouterAll();
        } else {
            menuList = sysMenuMapper.getRouter(roleId);
        }
        List<SysMenu> treeMenuList = getTreeMenuList(menuList);
        return treeRouter(treeMenuList);
    }

    /**
     * 递归获取路由
     *
     * @param menuList
     * @return
     */
    public List<Router> treeRouter(List<SysMenu> menuList) {
        List<Router> routerList = new ArrayList<>();
        for (SysMenu menu : menuList) {
            List<Router> routerChildren = new ArrayList<>();
            List<SysMenu> children = menu.getChildren();
            if (!children.isEmpty()) {
                routerChildren.addAll(treeRouter(children));
            }
            Router router = new Router();
            if (menu.getParentId() == 0) {
                router.setComponent("Layout");
            } else {
                router.setComponent(menu.getComponent());
            }
            if (!routerChildren.isEmpty()) {
                router.setChildren(routerChildren);
            }
            router.setPath(menu.getPath());
            router.setName(menu.getMenuName());
            router.setMeta(new Meta(menu.getMenuName(), menu.getIcon()));
            routerList.add(router);
        }
        return routerList;
    }

    /**
     * 添加菜单
     *
     * @param sysMenu
     * @return
     */
    @Override
    public boolean addMenu(SysMenu sysMenu) {
        return sysMenuMapper.addMenu(sysMenu) > 0;
    }

    /**
     * 删除菜单
     *
     * @param id
     * @return
     */
    @Override
    public boolean deleteMenu(Long id) {
        return sysMenuMapper.deleteMenu(id) > 0;
    }

    /**
     * 修改菜单
     *
     * @param sysMenu
     * @return
     */
    @Override
    public boolean updateMenu(SysMenu sysMenu) {
        return sysMenuMapper.updateMenu(sysMenu) > 0;
    }

    /**
     * 根据id获取菜单
     *
     * @param id
     * @return
     */
    @Override
    public SysMenu getMenuById(Long id) {
        SysMenu menu = sysMenuMapper.getMenuById(id);
        menu.setChildren(null);
        return menu;
    }
}
